package entity;

import interaction.AttackIC;
import interaction.InteractionOption;
import interaction.ShopIC;
import interaction.UseItemIC;
import interaction.UseSkillIC;
import item.Requirements;

import java.util.ArrayList;

import loadsave.Memento;
import loadsave.loadsaveobject.Iterator;
import loadsave.loadsaveobject.LoadSaveCollectionComposite;
import loadsave.loadsaveobject.LoadSaveIndex;
import loadsave.loadsaveobject.LoadSaveObject;
import loadsave.loadsaveobject.LoadSaveObjectComposite;
import loadsave.loadsaveobject.LoadSavePrimitive;
import occupation.Occupation;

public class WorldNPC extends Entity {
	
	private ArrayList<BattleNPC> spawningNPC;
	private InteractionOption commands;
	private boolean friendly;

	public WorldNPC() { super(); }
	
	public WorldNPC(String name, Occupation occ) {
		super(name, occ);
		spawningNPC = new ArrayList<BattleNPC>();
		friendly = false;
		AttackIC aic = new AttackIC("Attack");
		aic.setSpawningEnemies(spawningNPC);
		commands = new InteractionOption("");
		commands.addIntCommand(aic);
		commands.addIntCommand(new UseItemIC("UseItem"));
		commands.addIntCommand(new UseSkillIC("UsesKILL"));
		commands.addIntCommand(new ShopIC("Shop"));
	}
	public ArrayList<BattleNPC> getSpawningNPC() {
		return spawningNPC;
	}

	public void addSpawningNPC(BattleNPC npc) {
		spawningNPC.add(npc);
	}
	public void removeSpawningNPC(BattleNPC npc) {
		spawningNPC.remove(npc);
	}
	
	public InteractionOption getInteractionOptions() {
	    return commands;
	}
	
	public void setFriendly(boolean value) {
		friendly = value;
	}
	
	public boolean getFriendly() {
		return friendly;
	}
	
	@Override
	public boolean checkReqs(Requirements req) {
		return false;
	}

	@Override
	public Memento getMemento() {
		return new Memento() {

			@Override
			public LoadSaveObject getState() {
				LoadSaveObjectComposite toReturn = new LoadSaveObjectComposite("", "Entity");
				Iterator<LoadSaveObject> i = WorldNPC.super.getMemento().getState().getIterator();
				for (i.init(); i.hasNext(); i.next()) {
					toReturn.add(i.getCurrKey().getValue(), i.getCurrVal());
				}
				LoadSaveObject lso = commands.getMemento().getState();
				lso.setName("commands");
				toReturn.add(lso.getName(), lso);
				LoadSaveCollectionComposite lscc = new LoadSaveCollectionComposite("spawningNPC", "ArrayList", "BattleNPC");
				int index = 0;
				for (BattleNPC npc : spawningNPC) {
					lscc.add(new LoadSaveIndex(index++), npc.getMemento().getState());
				}
				toReturn.add("friendly", new LoadSavePrimitive("friendly", "boolean", String.valueOf(friendly)));
				return toReturn;
			}

			@Override
			public void restoreState(LoadSaveObject lso) {
				WorldNPC.super.getMemento().restoreState(lso);
				commands = new InteractionOption("");
				commands.getMemento().restoreState(lso.get("commands"));
				spawningNPC = new ArrayList<BattleNPC>();
				Iterator<LoadSaveObject> i = lso.get("spawningNPC").getIterator();
				for (i.init(); i.hasNext(); i.next()) {
					BattleNPC npc = new BattleNPC();
					npc.getMemento().restoreState(i.getCurrVal());
					spawningNPC.add(npc);
				}
				friendly = Boolean.valueOf(lso.get("friendly").getValue()); 
			}
			
		};
	}

}
